package com.timi_licai.timi_licai_account.adapter;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.model.ConsumeDetails;
import com.timi_licai.timi_licai_account.model.SampleModel;
import com.timi_licai.timi_licai_account.utils.DatabaseQueryUtils;
import com.timi_licai.timi_licai_account.utils.DateUtils;

import java.util.List;

/**
 * Created by licheng  on 2015/3/11.
 * email 635408136@qq.com
 */
public class TimeLineBaseAdapter extends BaseAdapter implements Animator.AnimatorListener {
    private Context context;
    private List<ConsumeDetails> data;
    //存储 position 用于typeImg判断是否点击的是同一个
    private int location = -1;

    public TimeLineBaseAdapter(Context context, List<ConsumeDetails> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {

        int ret = 0;
        if (data != null) {
            ret = data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        if (data == null) {
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        int type = getItemViewType(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            if (type == 1) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_main_list, parent, false);
                viewHolder.head = (RelativeLayout) convertView.findViewById(R.id.time_line_title);
                viewHolder.line = convertView.findViewById(R.id.time_line);
                viewHolder.time = (TextView) convertView.findViewById(R.id.time_line_date);
                viewHolder.typeName = (TextView) convertView.findViewById(R.id.time_line_typename);
                viewHolder.price = (TextView) convertView.findViewById(R.id.time_line_price);
                viewHolder.message = (TextView) convertView.findViewById(R.id.time_line_message);
                viewHolder.typeImg = (ImageView) convertView.findViewById(R.id.time_line_typeimg);
                viewHolder.Img = (ImageView) convertView.findViewById(R.id.time_line_img);

                //飞出的编辑和删除按钮
                viewHolder.delete = (ImageView) convertView.findViewById(R.id.time_line_delete);
                viewHolder.edit = (ImageView) convertView.findViewById(R.id.time_line_edit);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_main_list2, parent, false);
                viewHolder.head = (RelativeLayout) convertView.findViewById(R.id.time_line_title);
                viewHolder.line = convertView.findViewById(R.id.time_line);
                viewHolder.time = (TextView) convertView.findViewById(R.id.time_line_date);
                viewHolder.typeName = (TextView) convertView.findViewById(R.id.time_line_typename);
                viewHolder.price = (TextView) convertView.findViewById(R.id.time_line_price);
                viewHolder.message = (TextView) convertView.findViewById(R.id.time_line_message);
                viewHolder.typeImg = (ImageView) convertView.findViewById(R.id.time_line_typeimg);
                viewHolder.Img = (ImageView) convertView.findViewById(R.id.time_line_img);

                //飞出的编辑和删除按钮
                viewHolder.delete = (ImageView) convertView.findViewById(R.id.time_line_delete);
                viewHolder.edit = (ImageView) convertView.findViewById(R.id.time_line_edit);
                convertView.setTag(viewHolder);
            }

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //时间轴竖线的layout
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.line.getLayoutParams();
        //第一条数据，显示时间标题
        if (position == 0) {
            viewHolder.head.setVisibility(View.VISIBLE);
            viewHolder.time.setText(DateUtils.format("yyyy.MM.dd", data.get(position).getCreate_time()));
            params.addRule(RelativeLayout.ALIGN_TOP, R.id.time_line_title);

            params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.time_line_typeimg);

        } else { // 不是第一条数据
            // 本条数据和上一条数据的时间戳相同，时间标题不显示
            if (data.get(position).getCreate_time()
                    .equals(data.get(position - 1).getCreate_time())) {
                viewHolder.head.setVisibility(View.GONE);


                params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.time_line_typeimg);
            } else {
                //本条数据和上一条的数据的时间戳不同的时候，显示数据
                viewHolder.head.setVisibility(View.VISIBLE);
                viewHolder.time.setText(DateUtils.format("dd",
                        data.get(position).getCreate_time()) + "日");
                params.addRule(RelativeLayout.ALIGN_TOP, R.id.time_line_title);

                params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.time_line_typeimg);
            }
        }


        //赋值
        ConsumeDetails consumeDetails = data.get(position);
        viewHolder.line.setLayoutParams(params);
        int ioChoose = consumeDetails.getIoChoose();
        if (ioChoose == 1) {
            viewHolder.typeName.setText("收入");
        } else {
            String nameByTypeId = DatabaseQueryUtils.getNameByTypeId(consumeDetails.getType_id());
            viewHolder.typeName.setText(nameByTypeId);
        }
        viewHolder.price.setText(consumeDetails.getFee() + "");
        viewHolder.message.setText(consumeDetails.getDescription());


//TODO------------------------------------监听事件---------------------------------------------


        //设置typeImg点击后飞出删除和编辑按钮

        final ImageView typeImg = viewHolder.typeImg;
        final ImageView Img = viewHolder.Img;
        final ImageView delete = viewHolder.delete;
        final ImageView edit = viewHolder.edit;
        final TextView typeName = viewHolder.typeName;
        final TextView price = viewHolder.price;
        final TextView message = viewHolder.message;

        //  animator1 = AnimatorInflater.loadAnimator(context, R.animator.time_line_typeimg_move);


        //typeImg 的点击事件
        typeImg.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

//                // 数据的个数
//                for(int i=1;i<11;i++) {
//                    if (i != position) {
//                        View childItem = parent.getChildAt(i);
//                        if (childItem != null) {
//                            final View childDelete = childItem.findViewById(R.id.time_line_delete);
//                            final View childEdit = childItem.findViewById(R.id.time_line_edit);
//                            final TextView childPrice = (TextView) childItem.findViewById(R.id.time_line_price);
//                            final ImageView childImg = (ImageView) childItem.findViewById(R.id.time_line_img);
//                            final TextView childTypeName = (TextView) childItem.findViewById(R.id.time_line_typename);
//                            final TextView childMessage = (TextView) childItem.findViewById(R.id.time_line_message);
//
//                            if (childDelete != null) {
//                                if (childDelete.getVisibility() == View.VISIBLE && childEdit.getVisibility() == View.VISIBLE) {
//                                    childDelete.setVisibility(View.GONE);
//                                    childImg.setVisibility(View.VISIBLE);
//                                    ObjectAnimator  animator1_1 = ObjectAnimator.ofInt(childDelete, "left",  parent.getWidth() / 2 - 200,parent.getWidth() / 2);
//                                    animator1_1.setDuration(500);
//                                    animator1_1.start();
//                                }
//                            }
//                        }
//                    }
//
//                }
                if (location == position) {
                    //左边收入动画处理
                    if (delete.getVisibility() == View.VISIBLE) {
                        ObjectAnimator animator1_1 = ObjectAnimator.ofInt(delete, "left", parent.getWidth() / 2 - 200, parent.getWidth() / 2);
                        animator1_1.setDuration(500);
                        animator1_1.start();
                        //完成时的监听--隐藏图标
                        animator1_1.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                delete.setVisibility(View.GONE);
                                Img.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });

                    } else {
                        //左边弹出动画处理
                        delete.setVisibility(View.VISIBLE);
                        Img.setVisibility(View.GONE);
                        ObjectAnimator animator1 = ObjectAnimator.ofInt(delete, "left", parent.getWidth() / 2, parent.getWidth() / 2 - 200);
                        animator1.setDuration(500);
                        animator1.start();
                    }


                    //右边收入动画处理
                    if (edit.getVisibility() == View.VISIBLE) {

                        ObjectAnimator animator2_1 = ObjectAnimator.ofInt(edit, "left", parent.getWidth() / 2 + 160, parent.getWidth() / 2);
                        animator2_1.setDuration(500);
                        ObjectAnimator animator3_2 = ObjectAnimator.ofInt(edit, "right", parent.getWidth() / 2 + 160 + 80, parent.getWidth() / 2 + 80);
                        animator3_2.setDuration(500);
                        AnimatorSet set = new AnimatorSet();
                        set.play(animator2_1).with(animator3_2);
                        set.start();
                        set.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                typeName.setVisibility(View.VISIBLE);
                                price.setVisibility(View.VISIBLE);
                                message.setVisibility(View.VISIBLE);
                                edit.setVisibility(View.GONE);
                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });
                    } else {
                        //右边弹出动画处理
                        typeName.setVisibility(View.GONE);
                        price.setVisibility(View.GONE);
                        message.setVisibility(View.GONE);
                        edit.setVisibility(View.VISIBLE);
                        ObjectAnimator animator2 = ObjectAnimator.ofInt(edit, "left", parent.getWidth() / 2, parent.getWidth() / 2 + 160);
                        animator2.setDuration(500);
                        ObjectAnimator animator3 = ObjectAnimator.ofInt(edit, "right", parent.getWidth() / 2 + 80, parent.getWidth() / 2 + 160 + 80);
                        animator3.setDuration(500);
                        AnimatorSet set = new AnimatorSet();
                        set.play(animator2).with(animator3);
                        set.start();
                    }


                } else {
                    //左边弹出动画处理
                    delete.setVisibility(View.VISIBLE);
                    Img.setVisibility(View.GONE);
                    ObjectAnimator animator1 = ObjectAnimator.ofInt(delete, "left", parent.getWidth() / 2, parent.getWidth() / 2 - 200);
                    animator1.setDuration(500);
                    animator1.start();


                    //右边弹出动画处理
                    typeName.setVisibility(View.GONE);
                    price.setVisibility(View.GONE);
                    message.setVisibility(View.GONE);
                    edit.setVisibility(View.VISIBLE);
                    ObjectAnimator animator2 = ObjectAnimator.ofInt(edit, "left", parent.getWidth() / 2, parent.getWidth() / 2 + 160);
                    animator2.setDuration(500);
                    ObjectAnimator animator3 = ObjectAnimator.ofInt(edit, "right", parent.getWidth() / 2 + 80, parent.getWidth() / 2 + 160 + 80);
                    animator3.setDuration(500);
                    AnimatorSet set = new AnimatorSet();
                    set.play(animator2).with(animator3);
                    set.start();
                }
                location = position;


            }
        });


        //TODO------------------------------------图片优化------------------------------------
        // 类型图片：
        //imgUrl

        typeImg.setTag("typeimg");
        // 预设图片
        typeImg.setImageResource(R.drawable.ic_launcher);
        if (typeImg.getTag() != null && typeImg.getTag().equals("typeimg")) {
            // viewHolder.typeImg.setImageBitmap(result);
            typeImg.setImageResource(R.drawable.type_big_1);

        }


        //删除图片：
        delete.setTag(position);
        delete.setImageResource(R.drawable.item_delete);
        if (delete.getTag() != null && delete.getTag().equals(position)) {
            delete.setImageResource(R.drawable.item_delete);
            delete.setVisibility(View.GONE);

        }

        //编辑图片：
        edit.setTag(position);
        edit.setImageResource(R.drawable.item_edit);
        if (edit.getTag() != null && edit.getTag().equals(position)) {
            edit.setImageResource(R.drawable.item_edit);
            edit.setVisibility(View.GONE);
        }
        //用户图片：

        Img.setTag("img" + position);
        Img.setImageResource(R.drawable.ic_launcher);
        if (Img.getTag() != null && Img.getTag().equals("img" + position)) {
            Img.setVisibility(View.VISIBLE);
            typeName.setVisibility(View.VISIBLE);
            price.setVisibility(View.VISIBLE);
            message.setVisibility(View.VISIBLE);
            Img.setImageResource(R.drawable.item_pic_bg);


        }

        return convertView;
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    @Override
    public int getItemViewType(int position) {

        ConsumeDetails consumeDetails = data.get(position);
        if (consumeDetails.getIoChoose() == 1) {
            return 0;
        } else
            return 1;

    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public static class ViewHolder {
        public RelativeLayout head;
        public View line;
        public TextView time;
        public TextView typeName;
        public TextView price;
        public TextView message;
        public ImageView typeImg;
        public ImageView Img;
        public ImageView delete;
        public ImageView edit;

    }
}
